package 链表;

/**
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
 * 输入：head = [1,1,2,3,3]
 * 输出：[1,2,3]
 * <p>
 * 输入：head = [1,1,2]
 * 输出：[1,2]
 */
public class _83_删除排序链表中的重复元素 {
    public static void main(String[] args) {

        ListNode result = deleteDuplicates(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(3,new ListNode(2,new ListNode(1,null)))))));
        System.out.println(result);
    }
    public static ListNode deleteDuplicates(ListNode head) {

        if (head == null) return null;

        ListNode newNode = new ListNode(0);
        newNode.next = head;
        ListNode firstNode = newNode.next;

        while (firstNode.next != null) {

            if (firstNode.val == firstNode.next.val) {
                firstNode.next = firstNode.next.next;
            } else {
                firstNode = firstNode.next;
            }

        }


        return newNode.next;
    }
}
